package com.xy.p2p.action.front;



import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.octo.captcha.service.CaptchaServiceException;
import com.octo.captcha.service.image.ImageCaptchaService;
import com.xy.common.page.Pagination;
import com.xy.common.util.DictUtils;
import com.xy.common.security.BadCredentialsException;
import com.xy.common.security.UsernameNotFoundException;
import com.xy.common.web.CookieUtils;
import com.xy.common.web.RequestUtils;
import com.xy.core.web.WebErrors;

import com.xy.p2p.model.Tnoticeinfor;
import com.xy.p2p.service.TnoticeinforService;



@Controller
public class TnoticeinforAction{
	private static final Logger log = LoggerFactory.getLogger(TnoticeinforAction.class);
	public TnoticeinforAction() {
	}
 
	/**
	 * 公告信息表新增保存
	 * 
	 * @param tnoticeinfor
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/o_tnoticeinforSave.do")
	public String save(Tnoticeinfor tnoticeinfor, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateSave(tnoticeinfor, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		tnoticeinfor = tnoticeinforService.save(tnoticeinfor);
		return "redirect:v_tnoticeinforList.do";
	}

	/**
	 * 公告信息表删除
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("o_tnoticeinforDelete.do")
	public String delete(long[] ids) {
		if (ids != null) {
			for (long id : ids) {
				tnoticeinforService.remove(id);
			}
		}
		return "redirect:v_tnoticeinforList.do";
	}

	/**
	 * 公告信息表编辑页面
	 * 
	 * @param niid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tnoticeinforEdit.do")
	public String edit(long niid, HttpServletRequest request, ModelMap model) {
		Tnoticeinfor tnoticeinfor = tnoticeinforService.get(niid);
		model.addAttribute("tnoticeinfor", tnoticeinfor);
        setDict(model);
		return "tnoticeinforEdit";
	}

	/**
	 * 公告信息表详情
	 * 
	 * @param niid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tnoticeinforDetail.jspx")
	public String detail(long niid, HttpServletRequest request, ModelMap model) {
		Tnoticeinfor tnoticeinfor = tnoticeinforService.get(niid);
		model.addAttribute("tnoticeinfor", tnoticeinfor);
        setDict(model);
		return "/front/noticeDetail.html";
	}

	/**
	 * 公告信息表查询（分页）
	 * 
	 * @param tnoticeinfor
	 * @param pageNo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tnoticeinforList.jspx")
	public String listPage(Tnoticeinfor tnoticeinfor, Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		tnoticeinfor.setStatus(1);
		if (pageNo == null)
			pageNo = 1;
		Pagination pagination = tnoticeinforService.getTnoticeinforByPage(toMap(tnoticeinfor),
				pageNo, CookieUtils.getPageSize(request));
		List<Tnoticeinfor> list = (List<Tnoticeinfor>) pagination.getList();
		Collections.sort(list);
		pagination.setList(list);
		model.addAttribute("pagination", pagination);
        setDict(model);
		return "/front/about/aboutNotice.html";
	}

	/**
	 * 公告信息表查询
	 * 
	 * @param tnoticeinfor
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tnoticeinforList1.do")
	public String list(Tnoticeinfor tnoticeinfor, HttpServletRequest request, ModelMap model) {
		List<Tnoticeinfor> list = tnoticeinforService.getTnoticeinfor(toMap(tnoticeinfor));
		Collections.sort(list);
		model.addAttribute("list", list);
        setDict(model);
		return "tnoticeinforList";
	}


 	/***
	 * 将页面输入信息转成map
	 * 
	 * @param tnoticeinfor
	 * @return
	 */	private Map<String,Object> toMap(Tnoticeinfor tnoticeinfor){
		Map<String,Object> map=new HashMap<String, Object>();
      		if(tnoticeinfor.getNiid()!=null&&!tnoticeinfor.getNiid().equals("")){
			map.put("niid", tnoticeinfor.getNiid());
		}
		if(tnoticeinfor.getTitle()!=null&&!tnoticeinfor.getTitle().equals("")){
			map.put("title", tnoticeinfor.getTitle());
		}
		if(tnoticeinfor.getContent()!=null&&!tnoticeinfor.getContent().equals("")){
			map.put("content", tnoticeinfor.getContent());
		}
		if(tnoticeinfor.getNidate()!=null&&!tnoticeinfor.getNidate().equals("")){
			map.put("nidate", tnoticeinfor.getNidate());
		}
		if(tnoticeinfor.getKinds()!=null&&!tnoticeinfor.getKinds().equals("")){
			map.put("kinds", tnoticeinfor.getKinds());
		}
		if(tnoticeinfor.getStatus()!=null&&!tnoticeinfor.getStatus().equals("")){
			map.put("status", tnoticeinfor.getStatus());
		}
		if(tnoticeinfor.getUrl()!=null&&!tnoticeinfor.getUrl().equals("")){
			map.put("url", tnoticeinfor.getUrl());
		}
		if(tnoticeinfor.getRemark()!=null&&!tnoticeinfor.getRemark().equals("")){
			map.put("remark", tnoticeinfor.getRemark());
		}
		if(tnoticeinfor.getTuser()!=null){
			map.put("Tuser", tnoticeinfor.getTuser());
		}      
		return map;
	}
   
	/**
	 * 设置页面所需的数据字典
	 * @param model
	 */
	private void setDict(ModelMap model){
      model.addAttribute("kindsMap", DictUtils.getDictByType(DictUtils.TNOTICEINFOR_KINDS));
      model.addAttribute("statusMap", DictUtils.getDictByType(DictUtils.TNOTICEINFOR_STATUS));		
	}

	/**
	 * 数据校验
	 * @param model
	 */
	private WebErrors validateSave(Tnoticeinfor tnoticeinfor, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		/**if (errors.ifOutOfLength(username, "username", 1, 100)) {
			return errors;
		}  */    
		return errors;
	}

   
      
	@Resource
	private TnoticeinforService tnoticeinforService;   
}